package cn.snow.iot.module.warning.controller.admin.rule;


import cn.snow.iot.framework.common.pojo.CommonResult;
import cn.snow.iot.module.warning.controller.admin.rule.vo.WarningRuleVO;
import cn.snow.iot.module.warning.dal.dataobject.rule.WarningRuleDO;
import cn.snow.iot.module.warning.service.rule.IWarningRuleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Tag(name = "管理后台 - 告警规则")
@RestController
@RequestMapping("/warning/warningRule")
public class WarningRuleController {

    @Resource
    private IWarningRuleService warningRuleService;

    /**
     * 新增或修改
     *
     * @param warningDO
     * @return
     */
    @PostMapping("submit")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "新增或修改")
    public CommonResult<Object> submit(@RequestBody WarningRuleDO warningDO) {
        return warningRuleService.saveRule(warningDO);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "详情")
    public CommonResult<WarningRuleDO> detail(Long id) {
        return CommonResult.success(warningRuleService.getById(id));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "删除")
    public CommonResult<Boolean> remove(@RequestParam String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return CommonResult.success(warningRuleService.removeBatchByIds(idList));
    }

    /**
     * 分页
     *
     * @param ruleVO
     * @return
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "分页")
    public CommonResult<IPage<WarningRuleDO>> page(WarningRuleVO ruleVO) {
        IPage<WarningRuleDO> page = warningRuleService.selectWarningPage(ruleVO);
        return CommonResult.success(page);
    }
}

